clear
clear matrix
clear mata
program drop _all
set more off
set emptycells drop
set maxvar 100000
tempfile main

cd "<directory>"


*The code in this .do file reads in the dataset created in Data Cleaning Step 2.do and creates Table 1, Table 2, and Table 4;
u final_dataset,clear
tempfile mainforgraph
sa `mainforgraph',replace
sa `main',replace


u `main',clear
keep if dcountss_2==1 & yearmonth>=572 & yearmonth<=620 
keep if !missing(transfer, post, dspp0_5t8, dspp0_4t7, dspp0_6t9, dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep dosname masterloanidtrepp post transfer origyear orig_ltv securwac securocc orig_dscrnoi2 transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid myproptype myss msa transfer_nodlq2 ssid dpopp_indic_basedate treated_wl treated_dlq treated_dlqwl evertransfer_pre
collapse (max) origyear orig_ltv securwac securocc orig_dscrnoi2 transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid transfer_nodlq2 ssid dpopp_indic_basedate evertransfer_pre treated_wl treated_dlq treated_dlqwl (last) msa myproptype myss,by(dosname masterloanidtrepp)
drop if propid==.
foreach var of varlist dpopp_indic_base{
	replace `var'=0 if `var'==.
}

*	Table 1;
foreach var of varlist origyear orig_ltv securwac securocc orig_dscrnoi2 transfer transfer_fullpay2 transfer_nodlq2 treated_wl treated_dlq dpopp_indic_base {
	tabstat `var',stat(N mean p50 sd min max)
}


u `main',clear
keep if dcountss_2==1 & yearmonth>=572 & yearmonth<=620 
keep if !missing(transfer, post, dspp0_5t8, dspp0_4t7, dspp0_6t9, dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep dosname masterloanidtrepp post transfer origyear orig_ltv securwac securocc orig_dscrnoi2 transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid myproptype myss msa transfer_nodlq2 ssid dpopp_indic_basedate treated_wl treated_dlq treated_dlqwl evertransfer_pre evertransfer 
collapse (max) origyear orig_ltv securwac securocc orig_dscrnoi2 transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid transfer_nodlq2 ssid dpopp_indic_basedate evertransfer_pre treated_wl treated_dlq treated_dlqwl evertransfer (last) msa myproptype myss,by(dosname masterloanidtrepp)
drop if propid==.
foreach var of varlist dpopp_indic_base{
	replace `var'=0 if `var'==.
}

*	Table 4 Panel A, origination;
estpost ttest orig_dscrnoi2 orig_ltv securocc securwac,by(treated_wl) unequal
esttab .,wide
*	Table 4 Panel B, origination;
estpost ttest orig_dscrnoi2 orig_ltv securocc securwac,by(treated_dlq) unequal
esttab .,wide



tempfile rulechange
u `main',clear
keep if dcountss_2==1 & yearmonth>=572 & yearmonth<=620 
keep if !missing(transfer, post, dspp0_5t8, dspp0_4t7, dspp0_6t9, dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep dosname masterloanidtrepp post transfer origyear orig_ltv securwac securocc orig_dscrnoi2 ltv occrate dscrnoi age balratio transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid myproptype myss msa transfer_nodlq2 ssid dpopp_indic_basedate treated_wl treated_dlq treated_dlqwl evertransfer_pre distyear distyq distmonth evertransfer remterm distym

*	Table 4 Panel A, Aug 2009;
estpost ttest dscrnoi ltv occrate if distym==595,by(treated_wl) unequal
esttab .,wide
*	Table 4 Panel B, Aug 2009;
estpost ttest dscrnoi ltv occrate if distym==595,by(treated_dlq) unequal
esttab .,wide
sa `rulechange',replace


u `main',clear
keep if dcountss_2==1 & yearmonth>=572 & yearmonth<=620
keep if nouse2==0
keep if !missing(transfer, post, dspp0_5t8, dspp0_4t7, dspp0_6t9, dspp0_7t10, orig_ltv, securwac, securocc, orig_dscrnoi2, age, balratio, ltv, occrate, dscrnoi, propid, dealtypeid, originatorid, ssid, origyq, msayq)
keep dosname masterloanidtrepp transfer origyear orig_ltv securwac securocc orig_dscrnoi2 transfer dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 transfer_fullpay2 propid myproptype myss msa transfer_nodlq2 ssid dpopp_indic_basedate dpopp_indicdate dpopp_indic2date dpopp_indic3date treated_wl treated_dlq treated_dlqwl evertransfer_pre post dspp0_5t8 dspp0_4t7 dspp0_6t9 dspp0_7t10 age balratio ltv occrate dscrnoi remterm cum_dpo avg_dpo_volume
drop if propid==.
foreach var of varlist dpopp_indic_base dpopp_indic dpopp_indic2 dpopp_indic3 {
	replace `var'=0 if `var'==.
}

*	Table 2;
foreach var of varlist orig_ltv securwac securocc orig_dscrnoi2 age balratio ltv occrate dscrnoi transfer transfer_fullpay2 transfer_nodlq2 treated_wl treated_dlq treated_dlqwl post dspp0_5t8 dspp0_4t7 dspp0_6t9 dspp0_7t10 remterm cum_dpo avg_dpo_volume{
	tabstat `var',stat(N mean p50 sd min max)
}



